import cv2
image=cv2.imread('hat.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow("Input",image)
kernel1=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
eroded_img1=cv2.erode(image,kernel1)
cv2.imshow("Eroded(3*3)",eroded_img1)
kernel2=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
eroded_img2=cv2.erode(image,kernel2)
cv2.imshow("Eroded(5*5)",eroded_img2)

kernel1=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
dilated_img1=cv2.dilate(image,kernel1,iterations=1)
cv2.imshow("Eroded(3*3)",dilated_img1)
dilated_img2=cv2.dilate(image,kernel1,iterations=2)
cv2.imshow("2-dilate(3*3)",dilated_img2)

kerne1=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
image1=cv2.erode(image,kerne1)
image2=cv2.dilate(image,kerne1)
cv2.imshow("Output",image2)

kerne1=cv2.getStructuringElement(cv2.MORPH_RECT,(5,5))
image1=cv2.dilate(image,kerne1)
image2=cv2.erode(image1,kerne1)
cv2.imshow("Output",image2)

kerne1=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
gradient=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kerne1)
cv2.imshow("Input",image)
cv2.imshow("Gradient",gradient)

kerne1=cv2.getStructuringElement(cv2.MORPH_RECT,(3,3))
tophat=cv2.morphologyEx(image,cv2.MORPH_GRADIENT,kerne1)
blackhat=cv2.morphologyEx(image,cv2.MORPH_BLACKHAT,kerne1)
cv2.imshow("Input",image)
cv2.imshow("TopHat",tophat)
cv2.imshow("BlackHat",blackhat)
cv2.waitKey()
cv2.destroyAllWindows()